﻿<?php

//downErWeiMa($_GET['starTime'],$_GET['endTime'],$_GET['shengFeng']);
downExcelAll();

function downExcelAll(){

	$zip=new ZipArchive();

	$qure="select DISTINCT shengFen from everyDayTell WHERE riqi BETWEEN '{$_GET['starTime']}' AND '{$_GET['endTime']}' ";
	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);
	$dat=$shengFeng.date('Ymd');
	

	foreach ($arr as $key => $value) {
		
		echo $value['shengFen'].$dat;

		$nameFile=$value['shengFen'].$dat;

		downErWeiMa($_GET['starTime'],$_GET['endTime'],$value['shengFen']);

		$zip->open("excel/{$dat}.zip",ZipArchive::CREATE);   //打开压缩包

		$zip->addFile("excel/{$nameFile}.zip",basename("excel/{$nameFile}.zip"));

		$rs = $zip->close();

		unlink("excel/{$nameFile}.zip");



	}




}


function upDataErWeiMa($starTime,$endTime,$shengFeng){  //9


	$qure="select * from everyDayTell WHERE riqi BETWEEN '{$starTime}' AND '{$endTime}' AND shengFen='{$shengFeng}'";
	$host='localhost';
	$user='root';
	$password='root';
	$database='weizandata';
	$link=mysqli_connect($host, $user, $password, $database);
	$link->query("set names utf8");
	$result=mysqli_query($link, $qure);
	$arr=mysqli_fetch_all($result, 1);
	mysqli_close($link);
	$qure1="select DISTINCT city from everyDayTell WHERE riqi BETWEEN '{$starTime}' AND '{$endTime}' AND shengFen='{$shengFeng}'";
	$link1=mysqli_connect($host, $user, $password, $database);
	$link1->query("set names utf8");
	$result1=mysqli_query($link1, $qure1);
	$arr1=mysqli_fetch_all($result1, 1);

	$json = json_encode(array(
			"code"=>0,
			"msg"=>"",
			"data"=>$arr,
			"distinct"=>$arr1 ),
			JSON_UNESCAPED_UNICODE);

	mysqli_close($link1);

	return $json;


}




function downErWeiMa($starTime,$endTime,$shengFeng){ //9-1


	$zip=new ZipArchive();

	$dateArrJson=upDataErWeiMa($starTime,$endTime,$shengFeng);

	$dataArray=json_decode($dateArrJson,true);

	require_once 'PHPExcel-1.8/Classes/PHPExcel.php';

	$excelFiled=array('手机号码');

	//var_dump($dataArray);

	for($i=0;$i<count($dataArray['distinct']);$i++){

		$disCity=$dataArray['distinct'][$i]['city'];

		$objPHPExcel=new PHPExcel();

		$objPHPExcel->getActiveSheet()->fromArray($excelFiled,'A1');

		$row=1;

		for ($j=0; $j <count($dataArray['data']) ; $j++) { 
			
			 $City=$dataArray['data'][$j]['city'];

			 $tell=$dataArray['data'][$j]['tell'];

			 if($disCity==$City){

			 	//echo $City.':'.$tell.'<br>';

			 	$row+=1;

			 	$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $tell);

			 }

		}

		$objWriter =PHPExcel_IOFactory:: createWriter($objPHPExcel, 'Excel2007');

		$nameFile=$disCity.date("Ymd");

		$objWriter->save("excel/{$nameFile}.xlsx");  // //excel/二维码.xlsx

		$dat=$shengFeng.date('Ymd');

		$zip->open("excel/{$dat}.zip",ZipArchive::CREATE);   //打开压缩包

		$zip->addFile("excel/{$nameFile}.xlsx",basename("excel/{$nameFile}.xlsx"));

		$rs = $zip->close();

		unlink("excel/{$nameFile}.xlsx");

		unset($objWriter);
		unset($nameFile);
		unset($objPHPExcel);

	}


$dat=date('Ymd');
$filename="excel/{$dat}.zip";
header("Location: excel/{$dat}.zip");

}

?>